package com.wsoft.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.wsoft.core.service.CommonOrgService;
import com.wsoft.core.vo.OrganizationVO;
import com.wsoft.entity.BaseEntity;
import com.wsoft.hr.pm.service.sys.IHrOrganizationService;
import com.wsoft.system.entity.SysOrganizationEntity;
import com.wsoft.system.service.ISysOrganizationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author Zhang gukai
 * @Date 2024/11/29 9:05
 */
@Service
public class CommonOrgServiceImpl implements CommonOrgService {
    @Resource
    private ISysOrganizationService organizationService;
    @Resource
    private IHrOrganizationService hrOrganizationService;


    @Override
    public OrganizationVO getOrgInfo(Long orgId) {
        SysOrganizationEntity org = organizationService.getOrgById(orgId);
        return Convert.convert(OrganizationVO.class, org);
    }

    @Override
    public List<OrganizationVO> getParentOrg(Long orgId) {
        List<OrganizationVO> parentOrganizations = new ArrayList<>();
        findParentOrganizations(orgId, parentOrganizations);
        return parentOrganizations;
    }

    @Override
    public List<OrganizationVO> getOrganizationAll() {
        List<SysOrganizationEntity> list = organizationService.listAllOrgWithCache();
        return list.stream().map(item-> Convert.convert(OrganizationVO.class,item)).collect(Collectors.toList());
    }

    @Override
    public List<Long> getParOrgId(Long curOrgId,boolean haveOwn) {
        List<SysOrganizationEntity> list = organizationService.listAllOrgWithCache();
        List<Long> resultList = new ArrayList<>();
        //递归
            recursionOrg(list,curOrgId,resultList,SysOrganizationEntity::getPaId);
        if(!haveOwn && !resultList.isEmpty()){
            resultList.remove(0);
        }
        return resultList;
    }

    @Override
    public List<OrganizationVO> getChildOrg(Long orgId) {
        List<SysOrganizationEntity> list = organizationService.listAllOrgWithCache();
        SysOrganizationEntity org = organizationService.getOrgById(orgId);
        return list.stream().filter(e -> e.getAuthCode().startsWith(org.getAuthCode()))
                .map(item-> Convert.convert(OrganizationVO.class,item)).collect(Collectors.toList());
    }

    /**
     * 递归查找上级组织
     * @param list
     * @param orgId
     * @param resultList
     */
    private <T extends BaseEntity> void recursionOrg(List<T> list,
                                                    Long orgId,
                                                    List<Long> resultList,
                                                    Function<T, Long> parentIdGetter){
        List<T> collect =
                list.stream().filter(item -> orgId.equals(item.getId())).collect(Collectors.toList());
        if(CollUtil.isNotEmpty(collect)){
            resultList.add(collect.get(0).getId());
            T t = collect.get(0);
            recursionOrg(list,parentIdGetter.apply(t),resultList,parentIdGetter);
        }
    }



    private void findParentOrganizations(Long orgId, List<OrganizationVO> parentOrganizations) {
        OrganizationVO org = getOrgInfo(orgId);
        if (org == null) {
            return;
        }

        Long parentId = org.getPaId();
        if (parentId != 0) {
            OrganizationVO parentOrg = getOrgInfo(parentId);
            if (parentOrg != null) {
                parentOrganizations.add(parentOrg);
                findParentOrganizations(parentOrg.getId(), parentOrganizations);
            }
        }
    }

}
